#pragma once

#include <assert.h>

namespace dr
{
	template<class T>
	struct list_node
	{
		list_node* _prev;
		list_node* _next;
		T _data;

		list_node(const T& x = T())
		{
			_prev = nullptr;
			_next = nullptr;
			_data = x;
		}

	};

	/*typedef list_iterator<T, T&, T*> iterator;
	typedef list_iterator<T, const T&, const T*> const_iterator;*/
	template<class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T, Ref, Ptr> Self;

		list_iterator(Node* node)
			:_node(node)
		{}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		Self& operator++()
		{
			_node = _node->_next;

			return *this;
		}

		Self operator++(int)
		{
			Self tmp = *this;
			_node = _node->_next;
			return tmp;
		}

		Self& operator--()
		{
			_node = _node->_prev;

			return *this;
		}

		Self operator--(int)
		{
			Self tmp = *this;
			_node = _node->_prev;
			return tmp;
		}

		bool operator!=(const Self& lt) const
		{
			return _node != lt._node;
		}

		bool operator==(const Self& lt) const
		{
			return _node == lt._node;
		}

		Node* _node;
	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;

		void empt_init()
		{
			_head = new Node;
			_head->_prev = _head;
			_head->_next = _head;
			_size = 0;
		}
		
		list()
		{
			empt_init();
		}

		list(initializer_list<T> il)
		{
			//auto it = il.begin();
			//while (it != il.end())
			//{
			//	push_back(*it);
			//	it++;
			//}
			empt_init();
			for (auto& e : il)
			{
				push_back(e);
			}
		}

		~list()
		{
			clear();
			--_size;
		}

		// lt2(lt1)
		list(const list<T>& lt) // const list<T>& lt
		{
			empt_init();
			const_iterator it = lt.begin();
			while (it != lt.end())
			{
				push_back(*it);
				it++;
			}
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}

		list& operator= (list<T> lt)
		{
			swap(lt);

			return *this;
		}

		iterator insert(iterator pos, const T& x)
		{
			Node* next = pos._node;
			Node* node = new Node(x);
			Node* prev = next->_prev;

			node->_next = next;
			node->_prev = prev;
			next->_prev = node;
			prev->_next = node;

			return node;
		}

		iterator erase(iterator pos)
		{
			assert(pos._node != _head);
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;
			prev->_next = next;
			next->_prev = prev;

			delete cur;

			return next;
		}

		void push_back(const T& x)
		{
			//Node* tail = _head->_prev;
			//Node* node = new Node(x);
			//tail->_next = node;
			//node->_prev = tail;
			//node->_next = _head;
			//_head->_prev = node;
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		iterator begin()
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		const_iterator end() const
		{
			return _head;
		}

		size_t size() const
		{
			return _size;
		}

	private:
		Node* _head;
		size_t _size;
	};

	//void test01()
	//{
	//	dr::list<int> lt;
	//	lt.push_back(1);
	//	lt.push_back(2);
	//	lt.push_back(3);
	//	lt.push_back(4);
	//	lt.push_back(3);
	//	lt.push_back(2);
	//	lt.push_back('a');

	//	for (int& e : lt)
	//	{
	//		cout << e << " ";

	//	}
	//	cout << endl;

	//	list<int>::iterator it = lt.begin();
	//	while (it != lt.end())
	//	{

	//	}

	//	for (int& e : lt)
	//	{
	//		cout << e << " ";
	//	}
	//	cout << endl;
	//}
}